package com.sduept.nwld.dataserver.service.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.enums.SubStationTypeEnum;
import com.sduept.bigdata.equipment.assessment.exception.ParameterException;
import com.sduept.bigdata.equipment.assessment.model.standardlib.StandardInspection;
import com.sduept.bigdata.equipment.assessment.service.StandardInspectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangyu
 * @Date 2019年12月25日
 */
@Service
public class StandardInspectionCache {
	@Autowired
	private StandardInspectService standardService;

	/**
	 * 智能站所对应的所有的StandardInspection,Map<oederNo,StandardInspection>
	 */
	private Map<String,StandardInspection> standardMap_SMART = new HashMap<String, StandardInspection>();
	/**
	 * 常规站所对应的所有的StandardInspection,Map<oederNo,StandardInspection>
	 */
	private Map<String,StandardInspection> standardMap_CONVENTIONAL = new HashMap<String, StandardInspection>();
	
	@PostConstruct
	public void init() {
		standardMap_SMART.putAll(genStandardMapByStationType(SubStationTypeEnum.SMART));
		standardMap_CONVENTIONAL.putAll(genStandardMapByStationType(SubStationTypeEnum.CONVENTIONAL));
	}
	
	private Map<String, StandardInspection> genStandardMapByStationType(SubStationTypeEnum enumType) {
		Map<String,StandardInspection> map = new HashMap<String, StandardInspection>();
		try {
			List<StandardInspection> allStandards = standardService.findByStationType(enumType);
			for (StandardInspection stand : allStandards) {
				map.put(stand.getNodeNo(), stand);
			}
		} catch (ParameterException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public StandardInspection findStandardInspection(SubStationTypeEnum enumType,String orderNo) {
		if(StringUtils.isBlank(orderNo) || null == enumType) {
			return null;
		}
		if(enumType.equals(SubStationTypeEnum.SMART)) {
			return standardMap_SMART.get(orderNo);
		} else if(enumType.equals(SubStationTypeEnum.CONVENTIONAL)){
			return standardMap_CONVENTIONAL.get(orderNo);
		}
		return null;
	}
}
